import '../../../../presenter/PPBasePresenter.dart';
import '../../../../request/PPRequest.dart';
import '../model/PPShopHomePageModel.dart';

class PPShopHomePresenter extends PPBasePresenter {

    PPShopHomePresenter(): super();

    List<PPShopPlate> plateList = [];
     List<PPShopPlate> showPlateList = [];
    List<PPShopPlateContent> tabItems;
    PPShopPlateContent selTabItem;

    requestShopHomePlateInfo(Function callback) {
        PPRequest.request('/mall-front/plate/unEncryptFunc/info', {}, (bool isSucceed, dynamic result, dynamic otherInfo) {
            if(isSucceed) {
                _handlePlateInfoResponse(result);
                callback(true, showPlateList, null);
            } else {
                callback(false, result, result["msg"]);
            }
        }, (Map error){
            callback(false, null, error["errMsg"]);
        }, isEncryption: false);
    }

    requestFirstPageCmmdtyList(Function callback) {
        Map<String, String> param = {"plateContentId": selTabItem.id.toString(), "startRow": "1", "pageSize": "20", "plateType": "2",  "province": "江苏省", "city": "南京市"};
        PPRequest.request('/mall-front/commodity/unEncryptFunc/firstPageCmmdtyList', param, (bool isSucceed, dynamic result, dynamic otherInfo){
            if(isSucceed) {
                _handleCmmdtyListResponse(result);
                callback(true, showPlateList, null);
            } else {
                callback(false, result, result["msg"]);
            }
        }, (Map error){
            callback(false, null, error["errMsg"]);
        }, isEncryption: false);
    }

    _handlePlateInfoResponse(dynamic result) {
        List<dynamic> plateList = result["plateList"];
        PPShopPlate tabPlate;
        showPlateList = [];
        plateList = plateList.map((item){
            PPShopPlate plateModel = PPShopPlate.fromJson(item);
            if(plateModel.plateType == "05") {
                if(plateModel.contentList != null && plateModel.contentList.length > 0) {
                    tabPlate = plateModel;
                    selTabItem = selTabItem == null ? plateModel.contentList[0] : selTabItem;
                } else {
                    tabPlate = null;
                    selTabItem = null;
                }
            } else {
                showPlateList.add(plateModel);
            }
            return plateModel;
        }).toList();
        if(tabPlate != null) {
            showPlateList.add(tabPlate);
        }
    }
   
    _handleCmmdtyListResponse(dynamic result) {
        List<dynamic> cmmdtyInfosList = result["cmmdtyInfosList"];
        List<PPCommodityModel> list = cmmdtyInfosList.map((item){
            PPCommodityModel model = PPCommodityModel.fromJson(item);
            return model;
        }).toList();
        PPShopPlate plateModel = showPlateList.last;
        if(plateModel.plateType == "05") {
            showPlateList.last.cmmdtyList = list;
        }
    }

    
}

/**
 *  轮播 01
 *  分栏 02
 *  类目 03
 *  分期商城 04
 *  可滑动切换类别tab 05
 *  搜索框 06
 *  icon活动入口 07
 *  推荐(一排2个活动入口) 08
 *  限时热卖 09
*/